package com.pmcc.cj.socket;


import com.pmcc.core.config.MyEndpointConfigure;
import com.pmcc.core.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yaonan
 * Created by 姚楠 on 2019/3/16.
 * uuid = 用户的id
 * type = 0 代表客户端链接 type=1 代表页面链接
 */
@ServerEndpoint(value = "/dat/gk/{uuid}/{type}/{macAddress}", configurator = MyEndpointConfigure.class)
@Component
public class SocketServer {

    @Autowired
    EmployeeRepository employeeRepository;


    //静态变量，记录工控机链接数。
    private static int gkonlineCount = 0;

    //    聊天室内的所有人的session
    private static Map<String, SessionObject> gkSocketSet = new ConcurrentHashMap<>();



    class SessionObject {
        private String uuid;//用户id
        private Session session;//链接的session;

        public SessionObject(String uuid, Session session) {
            this.uuid = uuid;
            this.session = session;
        }

        public SessionObject() {
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public Session getSession() {
            return session;
        }

        public void setSession(Session session) {
            this.session = session;
        }
    }


    public static Map<String, SessionObject> getGkSocketSet() {
        return gkSocketSet;
    }

    public static void setGkSocketSet(Map<String, SessionObject> gkSocketSet) {
        SocketServer.gkSocketSet = gkSocketSet;
    }

    //与某个客户端的连接会话，需要通过它来给客户端发送数据
    private Session session;

    private String uuid;
    private String type;
    private String macAddress;


    /**
     * 连接建立成功调用的方法
     *
     * @param session 可选的参数。session为与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    @OnOpen
    public synchronized void onOpen(Session session, @PathParam(value = "uuid") String uuid, @PathParam(value = "type") String type, @PathParam(value = "macAddress") String macAddress) {
        this.session = session;
        this.uuid = uuid;
        this.type = type;
        this.macAddress = macAddress;

    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {

    }

    /**
     * 发生错误时调用
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("sessionid=" + session.getId() + "的用户发生了错误");
        onClose(session);
    }


    /**
     * 移除登陆session
     *
     * @param session
     */
    public void removeSession(Session session) {

    }


    /**
     * 收到客户端消息后调用的方法
     *
     * @param str     客户端发送过来的消息
     * @param session 可选的参数
     */
    @OnMessage
    public synchronized void onMessage(String str, Session session) {

    }




    public static synchronized int getGkOnlineCount() {
        return gkonlineCount;
    }

    public static synchronized void addGkOnlineCount() {
        SocketServer.gkonlineCount++;
    }

    public static synchronized void subGkOnlineCount() {
        SocketServer.gkonlineCount--;
    }


    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMacAddress() {
        return macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }
}
